<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/8
 * Time: 17:54
 */

namespace app\index\controller;


use app\index\model\BusLine;
use app\index\model\BusLineStation;
use app\index\model\GaoDeLine;
use app\index\model\GaoDeStation;
use think\image\Exception;
use think\Log;
use think\Request;

class GaoDeDemo extends BaseController
{


    public function getStationPoints()
    {

        $busStation = new BusLineStation();
        $stations = $busStation->all(['line_id' => 10]);
        $result = [];  //初始化一个数组
        foreach ($stations as $k => $v) {
            $result[$v['line_id']][] = $v;  //根据initial 进行数组重新赋值
        }
        echo json_encode($stations);
    }

    public function getBusLineName()
    {

        $busLine = new BusLine();
        $lineNames = $busLine->distinct(true)->column("line_name");
//        $stations=$busStation->;
//        $result =   [];  //初始化一个数组
//        foreach($stations as $k=>$v){
//            $result[$v['line_id']][]    =   $v;  //根据initial 进行数组重新赋值
//        }
        echo json_encode($lineNames, JSON_UNESCAPED_UNICODE);
    }

    public function uploadLine()
    {
        $line["company"] = $_POST["company"];
        $line["path"] = $_POST["path"];
        $line["total_name"] = $_POST["total_name"];
        $name=substr($line["total_name"] ,0,strpos($line["total_name"] ,"("));
        $line["id"] = $_POST["id"];
        $line["name"] = $name;
        try {
            $gaoDeLine = new GaoDeLine();
            $gaoDeLine->startTrans();
            $data=$gaoDeLine->find($_POST["id"]);
            if ($data)
            {
                return;
            }
            GaoDeLine::create($line);
            $stations = json_decode($_POST["stations"], true);
            foreach ($stations as $station) {
                $location = $station["location"];
                unset($station["location"]);
                $station = array_merge($station, $location);
                $station["line_id"] = $_POST["id"];
                GaoDeStation::create($station);
            }
            $gaoDeLine->commit();
            exit(200);
        } catch (\Exception $e) {
            $gaoDeLine->rollback();
            Log::record($e->getMessage(),"error");
        }

    }

}